﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;

namespace MixScriptWeb.Member
{
    public partial class fGroupMst :cCommonWeb,IMIxCommon
    {
        private static DataTable dt;
        private static SortDirection sd;
        private static cReferences oReff;
        // private static Control[] ctl;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    string[] sheadertext = new string[] { "Group ID", "Group NAme", "Group Desc"};
                    string[] sdatavalue = new string[] { "groupid", "groupname", "groupdesc"};
                    //string[] sdatavalue = new string[] { "pkno", "saslno", "cuidbc", "cuid", "cuiddvlc", "cuidadlc", "dvdt", "cuslno", "cuitno", "oaitno", "pkqt", "tdms", "cuod", "fpdt" };
                    initGvColumn(gvdetail, sheadertext, sdatavalue);

                    addConfirmation(this, new Button[] { btnDelete }, new string[] { "Are You sure want to delete this record?" });
                    setSortExpression(gvdetail, new string[] { "groupid", "groupname", "groupdesc"});                    


                }

                mn = (MainMenu)this.Master;
                hideStatusBar();

                oReff = new cReferences("sy_usergroup", "groupid");

            }
            catch (Exception)
            {
                
                throw;
            }
        }
        #region IMIxCommon Members

        public bool isvalid()
        {
            //throw new NotImplementedException();
            if (txtGroupid.Text != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cReferences();
                string ss = "";

                if (isvalid())
                {
                    
                        if (oReff.isRecordExist("sy_usergroup", new string[] { "groupid" }, new object[] { txtGroupid.Text }))
                        {
                            if (oReff.EditRowDtDB(out errMsg, "sy_usergroup", new string[] { "groupid" }, new object[] { txtGroupid.Text }, false, txtGroupid.Text, txtGroupName.Text, txtGroupDesc.Text))
                            {
                                ShowStatusBar(errMsg, msgType.msgError);
                                btnFind_Click(btnFind, new EventArgs());
                                btnClear_Click(btnClear, new EventArgs());
                            }
                            else
                            {
                                ShowStatusBar(errMsg, msgType.msgError);
                            }


                        }
                        else
                        {
                            if (oReff.AddNewRowDB(out errMsg, "sy_usergroup", new int[] { }, new string[] { }, false, txtGroupid.Text, txtGroupName.Text, txtGroupDesc.Text))
                            {
                                ShowStatusBar(errMsg, msgType.msgError);
                                txtGroupid.Text = ss;
                                btnFind_Click(btnFind, new EventArgs());
                                btnClear_Click(btnClear, new EventArgs());
                            }
                            else
                            {
                                ShowStatusBar(errMsg, msgType.msgError);
                            }
                            //ShowStatusBar("Record Not Found");
                        }
                    
                }
                else
                {
                    ShowStatusBar(errMsg);
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }

 

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            
            try
            {
               // cek apakah user sudah pilih data yg akan dihapus
                if (txtGroupid.Text != string.Empty)
                {
                    //cek apakah data itu ada
                    if (oReff.isRecordExist("sy_usergroup", new string[] { "groupid" }, new object[] { txtGroupid.Text }))
                    {
                        oReff = new cReferences();

                        //coba hapus
                        //                                nama primary key                nilainya          nama tablenya   pesan error
                        if (oReff.deleteRowDB(new string[] { "groupid" }, new object[] { txtGroupid.Text }, "sy_usergroup", out errMsg))
                        {
                            //jika berhasil
                            btnClear_Click(this, new EventArgs());
                            btnFind_Click(this, new EventArgs());
                            ShowStatusBar(em.getError(8));
                        }
                        else
                        {   //jika gagal   
                            btnClear_Click(this, new EventArgs());
                            btnFind_Click(this, new EventArgs());
                            ShowStatusBar(errMsg);
                        }




                    }
                    else
                    {   //jika data tidak ditemukan
                        ShowStatusBar(em.getError(7));
                    }
                }
                else
                {
                    ShowStatusBar(em.getError(7));
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cReferences(); //new cUserManagement();
                dt = new DataTable();
                //TextBox tb = (TextBox)this.FindControl("ctl00$ContentPlaceHolder1$txtBankCode$txt");

                Control[] ctl = new Control[]{ txtGroupid, txtGroupName, txtGroupDesc};


                if (oReff.getInquiry(ref dt, genInqParam(ctl), "sy_usergroup", out errMsg))
                {

                }
                else
                {
                    ShowStatusBar("Record Not Found", msgType.msgError);
                }
                bindGridview(gvdetail, dt);




            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                hideStatusBar();
                errMsg = "";
                setValid(new MixScriptTB[] { txtGroupid, txtGroupName, txtGroupDesc});
                clearMixTB(new MixScriptTB[] { txtGroupid, txtGroupName, txtGroupDesc });

            }
            catch (Exception ex)
            {
                ShowStatusBar(ex.Message);
            }

        }

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                //oUM = new cUserManagement();
                //  btnNewState("Cancel");
                if (gvdetail.SelectedIndex > -1)
                {
                    txtGroupid.Text = gvdetail.SelectedRow.Cells[0].Text;
                    txtGroupName.Text = gvdetail.SelectedRow.Cells[1].Text;
                    txtGroupDesc.Text = gvdetail.SelectedRow.Cells[2].Text;
                   
                    //ini ngehapus &nbsp;
                    //removeNbsp(new MixScriptTB[] { txtBankCode, txtBankName, txtBankAddress, txtCity, txtCountry, txtAccBank });
                    removeNbsp(new MixScriptTB[] { txtGroupid, txtGroupName, txtGroupDesc });



                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvdetail, dataView);
        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dt);


        }

        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItemIndex == -1)
                    return;
                e.Row.Attributes.Add("onMouseOver", "this.style.cursor='pointer';");
                e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent(gvdetail, "Select$" + e.Row.RowIndex.ToString()));
                //e.Row.Attributes.Add("onclick", this.GetPostBackEventReference(gvdetail, "Select$" + e.Row.RowIndex.ToString())); //  (gvdetail, "Select$" + e.Row.RowIndex.ToString()));


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
    }
}